import Vue from 'vue'
import Vuex from 'vuex'
import { getToken, setToken, removeToken } from '../../static/js/auth'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: getToken(),
    userInfo: { // 保存用户信息
      username: null,
      nickname: null,
      mobile: null,
      avatar: null,
      token: null
    }
  },
  getters: {
    token: state => state.userInfo.token
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_BASEINFO: (state, data) => {
      state.userInfo = data
    },
    REMOVE_BASEINFO: (state) => {
      state.userInfo = null
    }
  },
  actions: {
    // 前端 登出
    FedLogOut ({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }

  }
})
